<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>两个列表的最小索引总和</title>
</head>

<body>
  <script>
    function array(list1, list2) {
      list2 = new Map(list2.map((v, i) => [v, i]))
      const n = list1.length
      let sum = Infinity
      let ans = []
      for (let i = 0; i < n; i++) {
        const x = list1[i]
        if (list2.has(x)) {
          const curr = i + list2.get(x)
          if (curr < sum) {
            sum = curr
            ans = [x]
          } else if (curr === sum) {
            ans.push(x)
          }
        }
      }
      return ans
    };
    console.log(array(["Shogun", "Tapioca Express", "Burger King", "KFC"], ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]))
  </script>
</body>

</html>